package com.metaera.app.ui.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.metaera.app.Constants;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LoginPresenter;
import com.metaera.app.controller.presenter.UserInfoPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.rx.ResponseObserver;
import com.metaera.app.data.rx.SchedulersTransformer;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.SpannableStringHelper;
import com.metaera.app.utils.image.GlideApp;
import com.metaera.app.utils.validated.CheckedRule;
import com.metaera.app.utils.validated.Validated;

/**
 * 注销账号
 */
public class AccountUnregisterActivity extends BaseActivity {

    private View mLayoutUnregister;

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_account_unregister;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ActivityTitleHelper.setTitle(this, R.string.cancellation_text);

        // init
        TextView tvUid = findViewById(R.id.tv_uid);
        ImageView ivAvatar = findViewById(R.id.iv_avatar);
        TextView tvNickname = findViewById(R.id.tv_nickname);
        Button buttonNext = findViewById(R.id.button_next);
        mLayoutUnregister = findViewById(R.id.layout_unregister);
        CheckBox checkBox = findViewById(R.id.check_box);
        Button buttonUnregister = findViewById(R.id.button_unregister);

        checkBox.setText(
                SpannableStringHelper.newBuilder()
                        .append("我已了解并同意")
                        .color(Color.parseColor("#000000"))
                        .append("《账号注销协议》")
                        .color(getColorForRes(R.color.colorTextLink))
                        .click(v -> startActivity(WebViewActivity.newIntent(this, Constants.LINK_AGREEMENT_SERVICE)))
                        .build()
        );

        // 加载用户信息
        UserDetailsResponse user = LocalConfigStore.getInstance().getUser();
        tvUid.setText("UID:" + user.getId());
        tvNickname.setText(user.getNickname());
        GlideApp.with(this).load(user.getAvatar_img()).avatar().into(ivAvatar);

        LoginPresenter loginPresenter = new LoginPresenter();
        loginPresenter.setModelAndView(new BasicNetworkRequestView<>(data -> finish()));

        // listener
        Validated.add(buttonNext, new CheckedRule(checkBox));
        buttonNext.setOnClickListener(v -> mLayoutUnregister.setVisibility(View.VISIBLE));
        buttonUnregister.setOnClickListener(v -> loginPresenter.logOff());
    }

    @Override
    public void onBackPressed() {
        if (mLayoutUnregister.getVisibility() == View.VISIBLE) {
            mLayoutUnregister.setVisibility(View.GONE);
            return;
        }

        super.onBackPressed();
    }

}
